package com.gzjh.basic;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ehpc.model.v20180412.AddNodesRequest;
import com.aliyuncs.ehpc.model.v20180412.AddNodesResponse;
import com.aliyuncs.ehpc.model.v20180412.AddUsersRequest;
import com.aliyuncs.ehpc.model.v20180412.AddUsersResponse;
import com.aliyuncs.ehpc.model.v20180412.CreateClusterRequest;
import com.aliyuncs.ehpc.model.v20180412.CreateClusterRequest.Application;
import com.aliyuncs.ehpc.model.v20180412.CreateClusterResponse;
import com.aliyuncs.ehpc.model.v20180412.DeleteJobsRequest;
import com.aliyuncs.ehpc.model.v20180412.DeleteJobsResponse;
import com.aliyuncs.ehpc.model.v20180412.DeleteNodesRequest;
import com.aliyuncs.ehpc.model.v20180412.DeleteNodesRequest.Instance;
import com.aliyuncs.ehpc.model.v20180412.DeleteNodesResponse;
import com.aliyuncs.ehpc.model.v20180412.DeleteUsersRequest;
import com.aliyuncs.ehpc.model.v20180412.DeleteUsersResponse;
import com.aliyuncs.ehpc.model.v20180412.ListJobsRequest;
import com.aliyuncs.ehpc.model.v20180412.ListJobsResponse;
import com.aliyuncs.ehpc.model.v20180412.ListNodesRequest;
import com.aliyuncs.ehpc.model.v20180412.ListUsersRequest;
import com.aliyuncs.ehpc.model.v20180412.ListUsersResponse;
import com.aliyuncs.ehpc.model.v20180412.ModifyUserGroupsRequest;
import com.aliyuncs.ehpc.model.v20180412.ModifyUserGroupsResponse;
import com.aliyuncs.ehpc.model.v20180412.SetAutoScaleConfigRequest;
import com.aliyuncs.ehpc.model.v20180412.SetAutoScaleConfigResponse;
import com.aliyuncs.ehpc.model.v20180412.SubmitJobRequest;
import com.aliyuncs.ehpc.model.v20180412.SubmitJobResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

public class HPCOperate {
	
	private static final String REGION_ID = "cn-shenzhen";
	private static final String ACCESS_KEY_ID = "LTAIWZWzzL4fyZUC";
	private static final String ACCESS_SECRET = "Qf3tlZeYX3uifVCD9ygxz7K9hCxJ7T";
	
	private static DefaultProfile profile = null;
	private static IAcsClient client = null;
	static {
		profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
		client = new DefaultAcsClient(profile);
	}
	
	public static void main(String[] args) {
		createCluster();
	}
	
	/**
	 * 删除任务
	 */
	public static void deleteJobs() {
		DeleteJobsRequest request = new DeleteJobsRequest();
		request.setRegionId("");
		request.setClusterId("");
		request.setJobs("");
		
		try {
			DeleteJobsResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询作业状态
	 */
	public static void listJobs() {
		ListJobsRequest request = new ListJobsRequest();
		request.setRegionId("");
		request.setClusterId("");
		try {
			ListJobsResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提交作业
	 */
	public static void submitJob() {
		SubmitJobRequest request = new SubmitJobRequest();
		request.setRegionId("");
		request.setClusterId("");
		request.setCommandLine("");
		request.setRunasUser("");
		request.setRunasUserPassword("");
		
		try {
			SubmitJobResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改用户组权限
	 */
	public static void modifyUserGroups() {
		ModifyUserGroupsRequest request = new ModifyUserGroupsRequest();
		request.setRegionId("");
		request.setClusterId("");
		
		List<ModifyUserGroupsRequest.User> users = new ArrayList<>();
		ModifyUserGroupsRequest.User user = new ModifyUserGroupsRequest.User();
		user.setName("");
		user.setGroup("");
		users.add(user);
		
		request.setUsers(users);
		
		try {
			ModifyUserGroupsResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询用户列表
	 */
	public static void listUsers() {
		ListUsersRequest request = new ListUsersRequest();
		request.setRegionId("");
		request.setClusterId("");
		try {
			ListUsersResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除用户
	 */
	public static void deleteUsers() {
		DeleteUsersRequest request = new DeleteUsersRequest();
		request.setRegionId("");
		request.setClusterId("");
		
		List<DeleteUsersRequest.User> users = new ArrayList<>();
		DeleteUsersRequest.User user = new DeleteUsersRequest.User();
		user.setName("");
		users.add(user );
		
		request.setUsers(users);
		
		try {
			DeleteUsersResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增用户
	 */
	public static void addUsers() {
		AddUsersRequest request = new AddUsersRequest();
		request.setRegionId("");
		request.setClusterId("");
		
		List<AddUsersRequest.User> users = new ArrayList<>();
		AddUsersRequest.User user = new AddUsersRequest.User();
		user.setGroup("users");//wheel users
		user.setName("");
		user.setPassword("");
		users.add(user);
		
		request.setUsers(users);
		
		try {
			AddUsersResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置弹性伸缩配置
	 */
	public static void setAutoScaleConfig() {
		SetAutoScaleConfigRequest request = new SetAutoScaleConfigRequest();
		request.setRegionId("");
		request.setClusterId("");
		// 设置可以自动扩容
		request.setEnableAutoGrow(true);
		// 设置可以自动缩容
		request.setEnableAutoShrink(true);
		request.setMaxNodesInCluster(10);
		try {
			SetAutoScaleConfigResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询集群节点列表
	 */
	public static void listNodes() {
		ListNodesRequest request = new ListNodesRequest();
		request.setRegionId("");
		request.setClusterId("");
	}
	
	/**
	 * 批量删除节点
	 */
	public static void deleteNodes() {
		DeleteNodesRequest request = new DeleteNodesRequest();
		request.setRegionId("");
		request.setClusterId("");
		List<Instance> instances = new ArrayList<>();
		Instance instance = new Instance();
		instance.setId("");
		instances.add(instance);
		request.setInstances(instances);
		
		try {
			DeleteNodesResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	// 批量新增计算节点
	public static void addNodes() {
		AddNodesRequest request = new AddNodesRequest();
		request.setRegionId("");
		request.setClusterId("");
		request.setCount(1);
		request.setImageOwnerAlias("system");
		try {
			AddNodesResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}

	// 创建E-HPC集群
	public static void createCluster() {
		CreateClusterRequest request = new CreateClusterRequest();
		// 设置地域为深圳地域
		request.setRegionId("cn-shenzhen");
		// 设置可用区
		request.setZoneId("cn-shenzhen-b");
		// 设置集群名称
		request.setName("test-cluster");
		// 设置描述
		request.setDescription("test ehpc");
		// 设置版本号
		request.setEhpcVersion("1.0.0");
		// 设置操作系统镜像标签
		request.setOsTag("CentOS_7.2_64");
		
		// 设置调度器类型
		request.setSchedulerType("pbs");
		// 设置域账号服务类型
		request.setAccountType("nis");
		
		List<Application> applications = new ArrayList<Application>();
		// 设置应用软件标签
		request.setApplications(applications);

		// 设置VPC 
		request.setVpcId("vpc-wz91ixulwzszebanko961");
		// 设置交换机
		request.setVSwitchId("vsw-wz99fhcq05k0buu5opd29");
		
		// 设置存储类型为NAS
		request.setVolumeType("nas");
		// 设置NAS服务ID
		request.setVolumeId("2b55d483b0");
		// 设置网络共享存储协议
		request.setVolumeProtocol("nfs");
		// 设置NAS挂载点
		request.setVolumeMountpoint("2b55d483b0-krx22.cn-shenzhen.nas.aliyuncs.com");
		// 设置挂载共享存储的远程目录
		request.setRemoteDirectory("/");
		
		// 设置为不开启高可用
		request.setHaEnable(false);
		
		// 设置付费类型为按量付费
		request.setEcsChargeType("PostPaid");
		
		// 设置管控节点类型
		request.setEcsOrderManagerInstanceType("ecs.n1.tiny");
		// 设置计算节点数
		request.setEcsOrderComputeCount(1);
		// 设置登录计算节点类型
		request.setEcsOrderComputeInstanceType("ecs.n1.tiny");
		// 设置登录节点数
		request.setEcsOrderLoginCount(1);
		// 设置登录节点类型
		request.setEcsOrderLoginInstanceType("ecs.n1.tiny");
		// 设置管控节点数
		request.setEcsOrderManagerCount(1);
		
		// 必填该参数，否则会报错“无效的参数”
		request.setPassword("lyl123!@#");
		
		try {
			CreateClusterResponse response = client.getAcsResponse(request);
			System.out.println(JSONObject.toJSONString(response));
		} catch (ClientException e) {
			e.printStackTrace();
		} 
	}

}
